package com.incar.chuxing.base;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.incar.chuxing.api.LoadStatus;


public class Resource<T> {
    @LoadStatus
    private int loadStatus;

    private int statusCode;
    @Nullable
    public String message;

    @Nullable
    public T data;

    private Throwable t;


    public Resource(@LoadStatus int loadStatus) {
        this.loadStatus = loadStatus;
    }

    public Resource(@LoadStatus int loadStatus, Throwable t) {
        this.loadStatus = loadStatus;
        this.t = t;
    }

    public Resource(@LoadStatus int loadStatus, int statusCode, @Nullable T data, @Nullable String message) {
        this.loadStatus = loadStatus;
        this.statusCode = statusCode;
        this.data = data;
        this.message = message;
    }


    /**
     * 开始加载
     */

    public static <T> Resource<T> onLoading() {
        return new Resource<>(LoadStatus.LOADING);
    }

    /**
     * 收到响应
     */
    public static <T> Resource<T> onLoaded(BaseResultBean<T> result) {
        if (result.getStatus() == 200) {
            //加载成功，业务status ==200
            return new Resource<>(LoadStatus.SUCCESS, 200, result.getAttachment(), result.getMessage());
        } else {
            //加载失败，业务status !=200
            return new Resource<>(LoadStatus.FAILURE, result.getStatus(), result.getAttachment(), result.getMessage());
        }
    }

    /**
     * 加载失败，网络或者服务器内部响应错误
     */
    public static <T> Resource<T> onLoadError(Throwable t) {
        return new Resource<>(LoadStatus.ERROR, t);
    }


    public void handle(@NonNull OnHandleCallback<T> callback) {
        switch (loadStatus) {
            case LoadStatus.LOADING:
                callback.onLoading();
                break;
            case LoadStatus.SUCCESS:
                callback.onStatusSuccess(data);
                break;
            case LoadStatus.FAILURE:
                callback.onStatusFailure(statusCode, message, data);
                callback.onLoadFailure(new Throwable("状态码非200"));
                break;
            case LoadStatus.ERROR:
                callback.onLoadError(t);
                callback.onLoadFailure(t);
                break;
        }
        if (loadStatus != LoadStatus.LOADING)
            callback.onLoadComplete();
    }


    public interface OnHandleCallback<D> {
        void onLoading();

        void onStatusSuccess(D data);

        //业务错误
        void onStatusFailure(int statusCode, String message, D data);

        //网络错误
        void onLoadError(Throwable error);

        //所有的错误（业务错误&网络错误）
        void onLoadFailure(Throwable error);

        void onLoadComplete();
    }


    public abstract static class OnCallBack<E> implements Resource.OnHandleCallback<E> {


        @Override
        public void onLoading() {

        }

        @Override
        public void onStatusSuccess(E data) {

        }

        @Override
        public void onStatusFailure(int statusCode, String message, E data) {

        }

        @Override
        public void onLoadError(Throwable error) {

        }

        @Override
        public void onLoadFailure(Throwable error) {

        }

        @Override
        public void onLoadComplete() {

        }

    }
}